package searchteam.server;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import searchteam.beans.stateless.SearchTeamFacadeRemote;

public class RMIManager {
	
	private static RMIManager managerInstance;
	
	/**
	 * Default InitialContextFactory to use.
	 */
	private static final String DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory";
	
	public static RMIManager getInstance() {
		if (managerInstance == null) {
			managerInstance = new RMIManager();
		}
		
		return managerInstance;
	}

	private RMIManager() {	
	}
	
	public SearchTeamFacadeRemote getSearchTeamFacade() {
		// Lookup the Remote Bean interface through JNDI
		Context initialContext;
		
		try {
			initialContext = getInitialContext();
			SearchTeamFacadeRemote facadeBean = (SearchTeamFacadeRemote) initialContext.lookup("searchTeamFacade");
			return facadeBean;
		} 
		catch (NamingException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * @return Returns the InitialContext.
	 * @throws NamingException If the Context cannot be created.
	 */
	private static Context getInitialContext() throws NamingException {

		// if user don't use jclient/client container
		// we can specify the InitialContextFactory to use
		// But this is *not recommended*.
		//Hashtable<String, Object> env = new Hashtable<String, Object>();
		//env.put(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory());

		// Usually a simple new InitialContext() without any parameters is sufficent.
		return new InitialContext();
		//return new InitialContext(env);
	}

	/**
	 * Returns a configurable InitialContextFactory classname.<br/>
	 * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
	 * @return Returns a configurable InitialContextFactory classname.
	 */
	private static String getInitialContextFactory() {
		String prop = System.getProperty("easybeans.client.initial-context-factory");
		// If not found, use the default
		if (prop == null) {
			prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
		}
		return prop;
	}

}
